# import request
# from lxml import etree
# url="http://www.baidu.com"
# head={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'}
#
# r=request.get(url=url,headers=head)
# r.encoding=r.apparent_encoding
# neri=r.text
#
# doc=etree.parse(neri)
# c=etree.HTML(doc)
# v=c.xpath('//*[@id="su"]')[0]
# print(v)




import requests
from lxml import etree
import csv
import pandas

def get_html(url):
    try:
        r=requests.get(url)
        r.encoding=r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as error:
        print(error)

def parser(html):
    html_tree=etree.HTML(html)
    title=html_tree.xpath('//*[@id="rankWrap"]/div[2]/ul/li/a/@title')
    href=html_tree.xpath('//*[@id="rankWrap"]/div[2]/ul/li/a/@href')
    out_dict={'歌名':title,'地址':href}
    return out_dict
def save_dict2csv(item,path):
    df=pandas.DataFrame(path)
    df.to_csv(item)

if __name__ == '__main__':
    url="http://www.bspider.top/kugou/"
    html=get_html(url)
    ds=parser(html)
    save_dict2csv(ds,'.csv')








































